package com.hexus.eve.actions;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.hexus.eve.entities.MarketGroup;
import com.hexus.eve.entities.Type;
import com.hexus.eve.utils.TranslationUtils;
import com.opensymphony.xwork2.ActionSupport;


public class DisplayItemInfo extends ActionSupport{
	private static final long serialVersionUID = 1L;

	static Logger logger = Logger.getLogger(DisplayItemInfo.class);
	
	private Integer id;
	private Type type;

	@Autowired
	private TranslationUtils translationUtils;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public String execute() {
		String result = SUCCESS;
		Session session = sessionFactory.openSession();
		try {
			type = (Type) session.load(Type.class, getId());
			session.lock(type, LockMode.NONE);
			
			if(type == null) {
				if(logger.isInfoEnabled())
					logger.info("Type item is null");
				
				result = NONE;
			} else {
				translationUtils.translate(type, getLocale());
			}
		} catch (HibernateException he) {
			
			result = NONE;
		}
		session.close();

		return result;
	}

	
	/* 
	 * Getters and Setters 
	 */
	 
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public Type getType() {
		return type;
	}


	public void setType(Type type) {
		this.type = type;
	}
	
	public List<MarketGroup> getMarketGroupParents(){
		List<MarketGroup> mg = type.getMarketGroup().getParentsAsList();
		translationUtils.translate(mg, getLocale());
		return mg;
	}
}
